from django.test.client import Client
from django.test import TestCase
from django.shortcuts import render_to_response

from inv.admin.models import *
from inv.utils.errors  import *
from inv.utils.trace  import *
import sys

class AddClassTest(TestCase):
    """Set of test cases to verify the add class url
    """
    def setUp(self):
        # Every test needs a client.
        self.cls1 = Class(nam='CLASS 1', enb=0)
        self.cls1.save()
        self.client = Client()
        
    def test_000(self):
        """Success
        """
        ADM.debug("Success", extra={'cls':self})
        response = self.client.post('/admin/add_class/', {'CLASS_NAME': 'class1', 'ENB':0})
        expected = render_to_response('ajax_response.json', err[1][0])
        self.assertEqual(response._container, expected._container)

    def test_001(self):
        """Empty class name
        """
        ADM.debug("Empty class name", extra={'cls':self})
        response = self.client.post('/admin/add_class/', {'CLASS_NAME': '   ', 'ENB':0})
        expected = render_to_response('ajax_response.json', err[1][1])
        self.assertEqual(response._container, expected._container)
        response = self.client.post('/admin/add_class/', {'CLASS_NAME': '', 'ENB':0})
        expected = render_to_response('ajax_response.json', err[1][1])
        self.assertEqual(response._container, expected._container)

    def test_002(self):
        """Class Name already exists
        """
        ADM.debug("Class name already exists", extra={'cls':self})
        response = self.client.post('/admin/add_class/', {'CLASS_NAME': 'class 1', 'ENB':0})
        expected = render_to_response('ajax_response.json', err[1][2])
        self.assertEqual(response._container, expected._container)

    def test_003(self):
        """Data Refining
        """
        ADM.debug("Data refining", extra={'cls':self})
        response = self.client.post('/admin/add_class/', {'CLASS_NAME': 'class    1', 'ENB':0})
        expected = render_to_response('ajax_response.json', err[1][2])
        self.assertEqual(response._container, expected._container)
        response = self.client.post('/admin/add_class/', {'CLASS_NAME': 'cLaSs  1', 'ENB':0})
        expected = render_to_response('ajax_response.json', err[1][2])
        self.assertEqual(response._container, expected._container)


class AddGroupTest(TestCase):
    """Set of test cases to verify add group url 
    """
    def setUp(self):
        # Every test needs a client.
        self.cls_enb = Class(nam='CLASS 1', enb=1)
        self.cls_enb.save()
        self.cls_dis = Class(nam='CLASS 2', enb=0)
        self.cls_dis.save()
        self.grp1 = Class_Group(nam='GROUP 1', cls_id=self.cls_enb.id,
           seq=1, enb=0)
        self.grp1.save()
        self.client = Client()

    def test_001(self):
        """Empty group name
        """
        ADM.debug("Empty Group Name", extra={'cls':self})
        response = self.client.post('/admin/add_group/',
           {'CLASS_ID': self.cls_enb.id, 'GROUP_NAME':'', 'SEQ' : 1, 'ENB' : 1})
        expected = render_to_response('ajax_response.json', err[2][1])
        self.assertEqual(response._container, expected._container)
        
    def test_002(self):
        """Group name already exists
        """
        ADM.debug("Group name already exists", extra={'cls':self})
        response = self.client.post('/admin/add_group/',
           {'CLASS_ID': self.cls_enb.id, 'GROUP_NAME':'GROUP 1',
            'SEQ' : 1, 'ENB' : 0})
        expected = render_to_response('ajax_response.json', err[2][2])
        self.assertEqual(response._container, expected._container)

    def test_003(self):
        """Invalid class id
        """
        ADM.debug("Invalid Class id", extra={'cls':self})
        response = self.client.post('/admin/add_group/',
           {'CLASS_ID': self.cls_enb.id + 100, 'GROUP_NAME':'GROUP 10',
            'SEQ' : 1, 'ENB' : 0})
        expected = render_to_response('ajax_response.json', err[2][3])
        self.assertEqual(response._container, expected._container)
        
    def test_004(self):
        """Success
        """
        ADM.debug("Success", extra={'cls':self})
        response = self.client.post('/admin/add_group/',
           {'CLASS_ID': self.cls_enb.id, 'GROUP_NAME':'GROUP 2',
            'SEQ' : 1, 'ENB' : 1})
        expected = render_to_response('ajax_response.json', err[2][0])
        self.assertEqual(response._container, expected._container)
        
    def test_005(self):
        """Data refining
        """
        ADM.debug("Success", extra={'cls':self})
        response = self.client.post('/admin/add_group/',
           {'CLASS_ID': self.cls_enb.id, 'GROUP_NAME':'GrOuP    1  ',
            'SEQ' : 1, 'ENB' : 0})
        expected = render_to_response('ajax_response.json', err[2][2])
        self.assertEqual(response._container, expected._container)
        
    def test_006(self):
        """Same group name under different classes
        """
        ADM.debug("Same group name under different classes",
           extra={'cls':self})
        response = self.client.post('/admin/add_group/',
           {'CLASS_ID': self.cls_dis.id, 'GROUP_NAME':'GROUP 1',
            'SEQ' : 1, 'ENB' : 0})
        expected = render_to_response('ajax_response.json', err[2][0])
        self.assertEqual(response._container, expected._container)
        
    def test_007(self):
        """Class disabled. Group Enabled
        """
        ADM.debug("Class disabled. Group Enabled",
           extra={'cls':self})
        response = self.client.post('/admin/add_group/',
           {'CLASS_ID': self.cls_dis.id, 'GROUP_NAME':'GROUP 1',
            'SEQ' : 1, 'ENB' : 1})
        expected = render_to_response('ajax_response.json', err[2][4])
        self.assertEqual(response._container, expected._container)

class AddMetaTest(TestCase):
    """Set of test cases to verify add meta url 
    """
    def setUp(self):
        # Every test needs a client.
        self.cls_enb = Class(nam='CLASS 1', enb=1)
        self.cls_enb.save()
        self.grp_enb = Class_Group(nam='GROUP 1', cls_id=self.cls_enb.id,
           seq=1, enb=1)
        self.grp_enb.save()
        self.grp_enb1 = Class_Group(nam='GROUP 2', cls_id=self.cls_enb.id,
           seq=1, enb=1)
        self.grp_enb1.save()
        self.met_enb = Class_Meta(nam='META 1', grp_id=self.grp_enb.id,
           cls_id=self.cls_enb.id, typ="INT", seq=1, enb=1,
           ptn_id=0, sel_id=None)
        self.met_enb.save()
        
        self.cls_dis = Class(nam='CLASS 2', enb=0)
        self.cls_dis.save()
        self.grp_dis = Class_Group(nam='GROUP 1', cls_id=self.cls_dis.id,
           seq=1, enb=0)
        self.grp_dis.save()
        self.met_dis = Class_Meta(nam='META 1', grp_id=self.grp_dis.id,
           cls_id=self.cls_dis.id, typ="INT", seq=1, enb=0,
           ptn_id=1, sel_id=None)
        self.met_dis.save()
        self.client = Client()

    def test_001(self):
        """Success
        """
        ADM.debug("Success", extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_enb.id,
            'META_NAME':'Meta 2', 'META_TYPE': 'CHAR', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][0])
        self.assertEqual(response._container, expected._container)
        
    def test_002(self):
        """Empty Meta name
        """
        ADM.debug("Empty Meta name", extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_enb.id,
            'META_NAME':'', 'META_TYPE': 'INT', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][1])
        self.assertEqual(response._container, expected._container)
        
    def test_003(self):
        """Meta name already exists
        """
        ADM.debug("Meta name already exists", extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_enb.id,
            'META_NAME':'META 1', 'META_TYPE': 'INT', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][2])
        self.assertEqual(response._container, expected._container)
        
    def test_004(self):
        """Data Refining
        """
        ADM.debug("Data Refining", extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_enb.id,
            'META_NAME':'MeTa   1  ', 'META_TYPE': 'INT', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][2])
        self.assertEqual(response._container, expected._container)
        
    def test_005(self):
        """Invalid Meta type
        """
        ADM.debug("Invalid Meta type", extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_enb.id,
            'META_NAME':'META 10', 'META_TYPE': 'INT1', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][4])
        self.assertEqual(response._container, expected._container)
        
    def test_006(self):
        """Invalid Group ID
        """
        ADM.debug("Invalid Group ID", extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_enb.id + 100,
            'META_NAME':'META 10', 'META_TYPE': 'INT1', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][3])
        self.assertEqual(response._container, expected._container)
        
    def test_007(self):
        """Group not enabled. Meta enabled
        """
        ADM.debug("Group not enabled. Meta enabled", extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_dis.id,
            'META_NAME':'META 10', 'META_TYPE': 'INT1', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][5])
        self.assertEqual(response._container, expected._container)
        
    def test_008(self):
        """Same class. Different group. Same meta name
        """
        ADM.debug("Same class. Different group. Same meta name",
           extra={'cls':self})
        response = self.client.post('/admin/add_meta/',
           { 'GROUP_ID': self.grp_enb1.id,
            'META_NAME':'META 1', 'META_TYPE': 'INT', 'SEQ':'1',
            'ENB':'1', 'SEL_ID': ''})
        expected = render_to_response('ajax_response.json', err[3][2])
        self.assertEqual(response._container, expected._container)
